package com.xiake.app.web.controller.dynamic;

import com.xiake.core.annotation.LoginUser;
import com.xiake.core.entity.dynamic.Dynamic;
import com.xiake.core.entity.dynamic.DynamicImage;
import com.xiake.core.entity.dynamic.DynamicReply;
import com.xiake.core.model.AppUserInfo;
import com.xiake.service.dynamic.DynamicImageService;
import com.xiake.service.dynamic.DynamicReplyService;
import com.xiake.service.dynamic.DynamicService;
import com.xiake.utils.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xiake.core.model.ReturnDataModel;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("dynamic")
public class DynamicController {

    @Autowired
    private DynamicService dynamicService;
    @Autowired
    private DynamicImageService dynamicImageService;

    @Autowired
    private DynamicReplyService dynamicReplyService;

    @RequestMapping(value = "list")
    public ReturnDataModel list() throws Exception {

        return null;
    }

    /**
     * TODO 保存动态
     *
     * @param userInfo
     * @param dynamicId
     * @param title
     * @param detail
     * @param images
     * @param type
     * @return
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public ReturnDataModel save(@LoginUser AppUserInfo userInfo, Integer dynamicId, String title, String detail, String images, Integer type) throws Exception {
        ReturnDataModel rdm = new ReturnDataModel(ReturnDataModel.RETURN_SUCCESS, "保存成功");
        Dynamic dynamic = new Dynamic();
        dynamic.setId(dynamicId);
        dynamic.setTitle(title);
        dynamic.setDetail(detail);
        dynamic.setType(type);
        dynamic.setCreaterId(userInfo.getId());
        dynamicService.save(dynamic);
        if (StringUtils.isNotBlank(images)) {
//			dynamicImageService.
            String imageArr[] = images.split(",");
            for (String image : imageArr) {
                DynamicImage dimage = new DynamicImage();
                dimage.setImgUrl(image);
                dimage.setDynamicId(dynamic.getId());
                dynamicImageService.save(dimage);
            }
        }
        Map<String, Object> data = new HashMap<>();
        data.put("id", dynamic.getId());
        rdm.setData(data);
        return rdm;
    }

    /**
     * TODO 查询详细
     *
     * @param userInfo
     * @param dynamicId
     * @return
     */
    @RequestMapping(value = "detail")
    @ResponseBody
    public ReturnDataModel detail(@LoginUser AppUserInfo userInfo, Integer dynamicId) throws Exception {
        Map<String, Object> sqlMap = new HashMap<>();
        sqlMap.put("userId", userInfo.getId());
        sqlMap.put("dynamicId", dynamicId);
        Dynamic dynamic = dynamicService.get(sqlMap);
        Map<String, Object> data = new HashMap<>();
        PageInfo pageInfo = new PageInfo(1, 10);
        sqlMap.put("pageInfo", pageInfo);
        List<DynamicReply> replies = dynamicReplyService.query(sqlMap, "ByDynamicId");
        long countReply = dynamicReplyService.count(sqlMap, "ByDynamicId");
        pageInfo.setTotalNum(countReply);
        pageInfo.calculateAllPage();
        data.put("dynamic", dynamic);
        data.put("replies", replies);
        data.put("hasNext", pageInfo.getAllPage() > pageInfo.getPageNo() ? 1 : 0);
        return new ReturnDataModel(ReturnDataModel.RETURN_SUCCESS, "获取成功", data);
    }

    @RequestMapping(value = "reply")
    @ResponseBody
    public ReturnDataModel reply(@LoginUser AppUserInfo userInfo, String content, Integer type, Integer dynamicId) throws Exception {
        DynamicReply reply = new DynamicReply();
        reply.setContent(content);
        reply.setType(type);
        reply.setUserInfoId(userInfo.getId());
        reply.setDynamicId(dynamicId);
        dynamicReplyService.save(reply);
        Map<String, Object> data = new HashMap<>();
        data.put("replyId", reply.getId());
        return new ReturnDataModel(ReturnDataModel.RETURN_SUCCESS, "回复成功", data);
    }

    /**
     * 回复分页
     *
     * @param userInfo
     * @param dynamicId
     * @param page
     * @return
     */
    @RequestMapping(value = "reply")
    @ResponseBody
    public ReturnDataModel replyList(@LoginUser AppUserInfo userInfo, Integer dynamicId, Integer page) throws Exception {
        if(page == null || page.intValue() == 0){
            page = 1;
        }
        Map<String, Object> sqlMap = new HashMap<>();
        sqlMap.put("userId", userInfo.getId());
        sqlMap.put("dynamicId", dynamicId);
        Map<String, Object> data = new HashMap<>();
        PageInfo pageInfo = new PageInfo(page, 10);
        sqlMap.put("pageInfo", pageInfo);
        List<DynamicReply> replies = dynamicReplyService.query(sqlMap, "ByDynamicId");
        long countReply = dynamicReplyService.count(sqlMap, "ByDynamicId");
        pageInfo.setTotalNum(countReply);
        pageInfo.calculateAllPage();
        data.put("replies", replies);
        data.put("hasNext", pageInfo.getAllPage() > page ? 1 : 0);
        return new ReturnDataModel(ReturnDataModel.RETURN_SUCCESS, "获取成功", data);
    }

}
